// pages/home-school-export/home-school-export.ts
interface Subject {
  subjectId: string,
  name: string
}
interface Grade {
  gradeId: string,
  name: string
}
interface Group {
  index: string,
  groupMember: {
    index: string,
    groupMember: {
      name: string,
      homework: boolean
    }
  },
}
Page({
  /**
   * 页面的初始数据
   */
  data: {
    subject: "选择课程",
    grade: "选择班级",
    student: "选择学生",
    subjectData: [] as Subject[],
    gradeData: [] as Grade[],
    studentData: [] as Group[],
    isShowSubject: false,
    isShowGrade: false,
    selectedSubjectIndex: -1,
    selectedGradeIndex: -1,
  },
  handleStudent() {
    wx.navigateTo({
      url: "/package-group/pages/home-school-export-student/home-school-export-student"
    })
  },
  handleSelected(e: any) {
    const { index, type } = e.currentTarget.dataset
    if (type === 'subject') {
      this.setData({
        selectedSubjectIndex: index,
        subject: this.data.subjectData[index].name
      })
    }
    if (type === 'grade') {
      this.setData({
        selectedGradeIndex: index,
        grade: this.data.gradeData[index].name
      })
    }
  },
  handleListShow(e: any) {
    const { type } = e.currentTarget.dataset
    if (type === 'subject') {
      this.setData({
        isShowSubject: !this.data.isShowSubject
      })
    }
    if (type === 'grade') {
      this.setData({
        isShowGrade: !this.data.isShowGrade
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(option) {
    const { student, judge } = option
    if (judge) this.setData({
      student: student
    })
    wx.request({
      url: "https://mock.presstime.cn/mock/656dbe322fef936d34630d40/yi-xiao-ping/home-group-document",
      success: (res: {
        data: {
          data: {
            subject: Subject[],
            grade: Grade[],
            group: Group[],
          }
          code: number
        }
      }) => {
        const { data } = res;
        const { data: documentData, code } = data;
        if (code === 200) {
          this.setData({
            subjectData: documentData.subject,
            gradeData: documentData.grade,
            studentData: documentData.group
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})